package com.ctrip.ec.api.admin.common;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by cannon on 2018/5/2.
 */
public class RequestContext {

    private  static  ThreadLocal<LoginToken> loginTokenThreadLocal = new ThreadLocal<>();

    private  static  ThreadLocal<ResponseHeader> resHeaderThreadLocal = new ThreadLocal<>();



    /**
     * 设置用户登录token
     * @param token
     * @throws Exception
     */
    public static  void setLoginToken(String token) throws  Exception{
        String content = Encryption.desDecript(token);
        String [] tmp= content.split("\\|");
        LoginToken loginToken = new LoginToken();

        loginToken.setUserId(Long.parseLong(tmp[0]));
        loginToken.setUserName(tmp[1]);
        loginToken.setClientIp(tmp[2]);
        if(tmp.length>3) {
            loginToken.setExpireAt(new Timestamp(Long.parseLong(tmp[3])));
            if(tmp.length >4){
                try {
                    loginToken.setStoreId(Long.parseLong(tmp[4]));
                }catch (Exception ex){
                    ex.printStackTrace();
                    loginToken.setStoreId(0L);
                }
                if(tmp.length>5){
                    loginToken.setAgentId(Long.parseLong(tmp[5]));
                }
            }
        }
        loginTokenThreadLocal.set(loginToken);
    }

    public  static  void clearLoginToken(){
        loginTokenThreadLocal.remove();
    }

    public  static  Long getUserId(){
        if(loginTokenThreadLocal.get() == null){
            return null;
        }
        else {
            return loginTokenThreadLocal.get().getUserId();
        }
    }

    public  static  String getUserName(){
        if(loginTokenThreadLocal.get()   == null)
            return null;
        else
            return loginTokenThreadLocal.get().getUserName();
    }

    public  static  String getClientIp(){
        if(loginTokenThreadLocal.get() == null)
            return null;
        else
            return loginTokenThreadLocal.get().getClientIp();
    }

    public  static  Long getStoreId(){
        if(loginTokenThreadLocal.get() == null)
            return null;
        else
            return loginTokenThreadLocal.get().getStoreId();
    }

    public  static  Long getAgentId(){
        if(loginTokenThreadLocal.get() == null)
            return null;
        else
            return loginTokenThreadLocal.get().getAgentId();
    }

    public  static  void setResponseHeaderCount(long count){
        createHeader();
        resHeaderThreadLocal.get().setCount(count);
    }

    public  static  void setResponseHeaderMessage(String message){
        createHeader();
        resHeaderThreadLocal.get().setMessage(message);
    }

    public static  void setResponseHeaderStatus(ResponseStatus status){
        createHeader();
        resHeaderThreadLocal.get().setStatus(status.getId());
    }

    public  static  void clearResponseHeader(){
        resHeaderThreadLocal.remove();
    }

    public  static  Long getResponseHeaderCount(){
        ResponseHeader header = resHeaderThreadLocal.get();
        if(header != null){
            return header.getCount();
        }
        else
        {
            return null;
        }
    }

    public  static  String getResponseHeaderMessage(){
        ResponseHeader header = resHeaderThreadLocal.get();
        if(header != null){
            return header.getMessage();
        }
        else
        {
            return null;
        }
    }

    public static  Integer getResponseHeaderStatus(){
        ResponseHeader header = resHeaderThreadLocal.get();
        if(header != null){
            return header.getStatus();
        }
        else
        {
            return null;
        }
    }

    private    static void createHeader(){
        if(resHeaderThreadLocal.get() == null){
            resHeaderThreadLocal.set(new ResponseHeader());
        }
    }

    public static  String generateToken(Long userId,String userName,String clientIp,Long storeId,Long agentId) throws Exception{
        //Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Calendar calendar = Calendar.getInstance(); //创建一个日历对象。
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,7);
        long timestamp = calendar.getTimeInMillis();
        String content= userId + "|" + userName + "|" + clientIp + "|" + timestamp + "|" + storeId + "|" + agentId;
        return Encryption.desEncript(content);
    }

    private static class  LoginToken{
        private String userName;
        private  long userId;
        private  String clientIp;
        private  Timestamp expireAt;
        private  long storeId;
        private  long agentId;


        public long getStoreId() {
            return storeId;
        }

        public void setStoreId(long storeId) {
            this.storeId = storeId;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public long getUserId() {
            return userId;
        }

        public void setUserId(long userId) {
            this.userId = userId;
        }


        public String getClientIp() {
            return clientIp;
        }

        public void setClientIp(String clientIp) {
            this.clientIp = clientIp;
        }



        public Timestamp getExpireAt() {
            return expireAt;
        }

        public void setExpireAt(Timestamp expireAt) {
            this.expireAt = expireAt;
        }

        public long getAgentId() {
            return agentId;
        }

        public void setAgentId(long agentId) {
            this.agentId = agentId;
        }
    }

    private static  class  ResponseHeader{
        private  Integer status;
        private  Long  count;
        private  String message;

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public Long getCount() {
            return count;
        }

        public void setCount(Long count) {
            this.count = count;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}
